package handler

import (
	"github.com/gin-gonic/gin"
	"month6/client/client"
	"month6/client/request"
	"month6/common/proto/user"
	"net/http"
)

func SuperLogin(c *gin.Context) {
	var data request.SuperLogin
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
		})
		return
	}

	login, err := client.SuperLogin(c, &user.SuperLoginReq{
		Name:     data.Name,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": login,
	})
}

func AddClass(c *gin.Context) {
	userId := c.GetUint("userId")
	var data request.AddClass
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
		})
		return
	}

	class, err := client.AddClass(c, &user.AddClassReq{
		Name:   data.Name,
		Price:  float32(data.Price),
		Cover:  data.Cover,
		Status: int64(data.Status),
		Num:    int64(data.Num),
		UserId: int64(userId),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": class,
	})
}

func ListClass(c *gin.Context) {
	class, err := client.ListClass(c, &user.ListClassReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": class,
	})
}

func DetailClass(c *gin.Context) {
	var data request.DetailClass
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
		})
		return
	}

	class, err := client.DetailClass(c, &user.DetailClassReq{Id: data.Id})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": class,
	})
}

func UpdatedClassStatus(c *gin.Context) {
	var data request.UpdatedClassStatus
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
		})
		return
	}

	status, err := client.UpdatedClassStatus(c, &user.UpdatedClassStatusReq{
		Id:     data.Id,
		Status: data.Status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": status,
	})
}

func AddRob(c *gin.Context) {
	var data request.AddRob
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
		})
		return
	}

	rob, err := client.AddRob(c, &user.AddRobReq{
		ClassId:  data.ClassId,
		NowPrice: float32(data.NowPrice),
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": rob,
	})
}

func UserBuyClass(c *gin.Context) {
	var data request.UserBuyClass
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
		})
		return
	}

	class, err := client.UserBuyClass(c, &user.UserBuyClassReq{
		Name:    data.Name,
		Num:     data.Num,
		ClassId: data.ClassId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": class,
	})
}

func BrowseList(c *gin.Context) {
	list, err := client.BrowseList(c, &user.BrowseListReq{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": list,
	})
}
func BrowseList2(c *gin.Context) {
	list, err := client.BrowseList2(c, &user.BrowseList2Req{})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": list,
	})
}

func AddOrder(c *gin.Context) {
	userId := c.GetUint("userId")
	var data request.AddOrder
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
		})
		return
	}

	order, err := client.AddOrder(c, &user.AddOrderReq{
		UserId:  int64(userId),
		ClassId: data.ClassId,
		PayType: data.PayType,
		Num:     data.Num,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": order,
	})
}

func AliPay(c *gin.Context) {
	out_trade_no := c.PostForm("out_trade_no")
	trade_status := c.PostForm("trade_status")
	pay, err := client.AliPay(c, &user.AliPayReq{
		OrderSn: out_trade_no,
		Status:  trade_status,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": pay,
	})
}
